package com.xxgc.eow.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xxgc.eow.entity.Choosecontent;
import com.xxgc.eow.entity.Topicanswer;
import com.xxgc.eow.service.IChoosecontentService;
import com.xxgc.eow.service.ITopicanswerService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import com.xxgc.eow.result.Result;

import java.time.LocalDateTime;
import java.util.List;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.xxgc.eow.service.IChoosetopicService;
import com.xxgc.eow.entity.Choosetopic;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 选择题 前端控制器
 * </p>
 *
 * @author Mr.Abel
 * @since 2023-05-18
 */
@Tag(name = "选择题")
@RestController
@RequestMapping("/choosetopic")
public class ChoosetopicController {

    @Resource
    private IChoosetopicService choosetopicService;

    @Resource
    private IChoosecontentService choosecontentService;

    @Resource
    private ITopicanswerService topicanswerService;

    /**
     * 查询所有接口
     * @return
     */
    @ApiOperationSupport(author = "yxh")
    @Operation(summary = "分页查询所有接口", description = "查询选择题所有数据")
    @SaIgnore
    @Parameters({
            @Parameter(name = "pageNum",description = "页码",example="1",required = true,in=ParameterIn.QUERY),
            @Parameter(name = "pageSize",description = "每页多少条",example="10",required = true,in=ParameterIn.QUERY)
    })
    @GetMapping
    public Result<List<Choosetopic>> findAll(Integer pageNum, Integer pageSize) {
        if(pageNum == null || pageNum <= 0){
            pageNum = 1;
        }
        if(pageSize == null || pageSize <= 0){
            pageSize = 10;
        }
        return Result.ok(choosetopicService.getAllChooseTopic(pageNum,pageSize));
    }

    /**
     * 根据id查询数据接口
     * @param id
     * @return
     */
    @ApiOperationSupport(author = "yxh")
    @Operation(summary = "根据id查询数据接口", description = "通过id查询")
    @Parameters({
            @Parameter(name = "id",description = "组件id",required = true,in=ParameterIn.QUERY)
    })
    @SaIgnore
    @GetMapping("/{id}")
    public Result<Choosetopic> findOne(@PathVariable Integer id) {
        return Result.ok(choosetopicService.getChooseTopic(id));
    }

    /**
     * 分页查询接口
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "分页查询接口", description = "分页查询")
    @Parameters({
            @Parameter(name = "pageNum",description = "页码",example="1",required = true,in=ParameterIn.QUERY),
            @Parameter(name = "pageSize",description = "每页多少条",example="10",required = true,in=ParameterIn.QUERY)
    })
    @SaIgnore
    @GetMapping("/page")
    public Result<Page<Choosetopic>> findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize) {
        QueryWrapper<Choosetopic> queryWrapper = new QueryWrapper<>();
        return Result.ok(choosetopicService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    /**
     * 新增和更新接口
     * @param
     * @return
     */
    @ApiOperationSupport(author = "yxh")
    @Operation(summary = "新增和更新接口(加强版)", description = "传入id为更新,不传为新增,时间和有效字段均不填,可以传入题解及选项")
    @SaIgnore
    @PostMapping("/saveOrUpdate")
    public Result save(@RequestBody Choosetopic choosetopic) {
        if(choosetopic == null){
            return Result.error("不准传null进来");
        }
        if(choosetopic.getChoosecontents().size() < 2){
            return Result.error(201,"选择题选项必须大于2","");
        }else if(choosetopic.getChooseLevel() == null || choosetopic.getChooseLevel() <= 0 || choosetopic.getChooseLevel() > 3){
            return Result.error(202,"选择题选项等级不能为空且等级为1-3级","");
        }else if(choosetopic.getChooseAnswer() == null || choosetopic.getChooseAnswer() == ""){
            return Result.error(203,"选择题题解不能为空","");
        }else if(choosetopic.getChooseTitle() == null || choosetopic.getChooseTitle() == ""){
            return Result.error(204,"选择题内容不能为空","");
        }
        if(choosetopic.getTip() == 1){
            choosetopic.setDestoryTime(LocalDateTime.now());
        }
        Topicanswer topicanswer = new Topicanswer();
        topicanswer.setTopicAnswerContent(choosetopic.getChooseAnswer());
        topicanswer.setTip(choosetopic.getTip());


        Integer id = choosetopic.getChooseId();
        choosetopic.setChooseAnswer(null);
        if(id == null){
            int i = choosetopicService.addChooseTopic(choosetopic);
            setChooseContents(choosetopic , id);
        }else {
            setChooseContents(choosetopic, id);
            choosetopic.setChoosecontents(null);
            choosetopicService.updateById(choosetopic);
        }
        if(topicanswerService.getTopicAnswerByTopicId(choosetopic.getChooseId()).size() > 0){
            topicanswer.setTopicAnswerId(topicanswerService.getTopicAnswerByTopicId(choosetopic.getChooseId()).get(0).getTopicAnswerId());
        }
        topicanswer.setTopicId(choosetopic.getChooseId());
        //这俩可能用不上，暂时这样子
        topicanswer.setTopicAnswerUser(1);
        topicanswer.setVideo("");
        topicanswerService.saveOrUpdate(topicanswer);

        return Result.ok();
    }

    private void setChooseContents(Choosetopic choosetopic, Integer id) {
        for(int i = 0; i < choosetopic.getChoosecontents().size(); i++){
            try {
                Choosecontent c = choosetopic.getChoosecontents().get(i);
                c.setChooseId(choosetopic.getChooseId());
                c.setTip(choosetopic.getTip());
                if(id == null){
                    choosecontentService.addChoosecontent(c);
                }else{
                    choosecontentService.updateChoosecontentByTopicId(c,i);
                }
            }catch (Exception e){}
        }
    }

    /**
     * 查询所有接口
     * @param libraryId
     * @return
     */
    @ApiOperationSupport(author = "yxh")
    @Operation(summary = "查询选择题", description = "通过题库id查询选择题")
    @Parameters({
            @Parameter(name = "libraryId",description = "题库id",required = true,in=ParameterIn.QUERY)
    })
    @SaIgnore
    @GetMapping("/get/{libraryId}")
    public Result<List<Choosetopic>> getTopicsByLibraryId(@PathVariable int libraryId) {
        return Result.ok(choosetopicService.getAllChooseTopicByLibrary(libraryId));
    }

    /**
     * 查询总数量
     * @return
     */
    @ApiOperationSupport(author = "yxh")
    @Operation(summary = "查询总数量", description = "查询题目总数")
    @SaIgnore
    @GetMapping("/getCount")
    public Result<Long> getCount() {
        return Result.ok(choosetopicService.count());
    }

    /**
     * 模糊查询接口
     * @return
     */
    @ApiOperationSupport(author = "yxh")
    @Operation(summary = "模糊查询", description = "根据对应属性进行模糊查询")
    @SaIgnore
    @Parameters({
            @Parameter(name = "pageNum",description = "页码",example="1",required = true,in=ParameterIn.QUERY),
            @Parameter(name = "pageSize",description = "每页多少条",example="10",required = true,in=ParameterIn.QUERY),
            @Parameter(name = "libraryName",description = "题库名称",example="题库1",required = false,in=ParameterIn.QUERY)
    })
    @GetMapping("/getByBlur")
    public Result findFuzzy(Choosetopic choosetopic, Integer pageNum, Integer pageSize,String libraryName) {
        if(pageNum == null || pageNum <= 0){
            pageNum = 1;
        }
        if(pageSize == null || pageSize <= 0){
            pageSize = 10;
        }
        if(libraryName == null){
            libraryName = "";
        }
        if(choosetopicService.getAllChooseTopicsByChooseTopic(choosetopic,pageNum,pageSize,libraryName).size() == 0){
            return Result.error(201,"未查询到相关数据","");
        }else{
            return Result.ok(choosetopicService.getAllChooseTopicsByChooseTopic(choosetopic,pageNum,pageSize,libraryName));
        }

    }

    /**
     * 模糊查询总条数接口
     * @return
     */
    @ApiOperationSupport(author = "yxh")
    @Operation(summary = "模糊查询总条数", description = "根据对应属性进行模糊查询总条数")
    @SaIgnore
    @Parameters({
            @Parameter(name = "libraryName",description = "题库名称",example="题库1",required = false,in=ParameterIn.QUERY)
    })
    @GetMapping("/getCountByBlur")
    public Result getCountByBlur(Choosetopic choosetopic,String libraryName) {
        if(libraryName == null){
            libraryName = "";
        }
        return Result.ok(choosetopicService.getCountByChooseTopic(choosetopic,libraryName));
    }

    /**
     * 通过难度查询总数
     * @param chooseLevel
     * @return
     */
    @ApiOperationSupport(author = "yxh")
    @Operation(summary = "通过难度查询总数", description = "通过难度查询选择题总数")
    @Parameters({
            @Parameter(name = "chooseLevel",description = "难度等级(1简单，2中等，3困难)",required = true,in=ParameterIn.QUERY)
    })
    @SaIgnore
    @GetMapping("/getByLevel")
    public Result getCountByChooseLevel(Integer chooseLevel) {
        if(chooseLevel == null || chooseLevel < 1 || chooseLevel > 3){
            return Result.error("难度等级错误");
        }
        Integer num = choosetopicService.getCountByChooseLevel(chooseLevel);
        if(num == null){
            num = 0;
        }
        return Result.ok(num);
    }
}